//
//  DebtDetailsViewController.m
//  TenThousand
//
//  Created by 我的 on 16/6/24.
//  Copyright © 2016年 baixue. All rights reserved.
//

#import "DebtDetailsViewController.h"

@interface DebtDetailsViewController ()<UITextFieldDelegate>
@property (nonatomic, strong) NSTimer *timer;;
@property (weak, nonatomic) IBOutlet UIButton *getCOde;

@end
static NSInteger countDownSeconds;

@implementation DebtDetailsViewController
{
    NSString    *phone;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"债权转让";
    [self createLeft:[UIImage imageNamed:@"ZHSZicon_03"] leftClick:^{
        [self.navigationController popViewControllerAnimated:YES];
    } right:nil rightClick:nil];
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(getValidateCodeCountDown) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantFuture]];

    [self createUI];
}
- (void)createUI {
    
    self.titleLabel.text = self.titleName;
    self.BenjinLB.text = [NSString stringWithFormat:@"%.2f",[self.BenjinStr floatValue]];
    self.ShouyiLB.text = [NSString stringWithFormat:@"%.2f",[self.ShouyiStr floatValue]];
    self.AllMoneyLB.text = [NSString stringWithFormat:@"%.2f",[self.AllStr floatValue]];
    self.OutBenjinLB.text = [NSString stringWithFormat:@"%.2f",[self.BenjinStr floatValue]];
    self.OutBenjinLB.enabled = NO;
    self.OutBenjinLB.delegate = self;
    self.OutShouyi.delegate = self;
    [self.Yanzheng addTarget:self action:@selector(yanzhengAction:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)yanzhengAction:(UIButton *)sender {
    if (!sender.selected) {
        //设置倒计时时间为60妙
        countDownSeconds = 60;
        _getCOde.tintColor = [UIColor clearColor];
        _getCOde.selected = YES;
        _getCOde.enabled = NO;
        _getCOde.backgroundColor = [UIColor colorWithRed:0.91 green:0.91 blue:0.91 alpha:1.00];

        //打开定时器
        [_timer setFireDate:[NSDate distantPast]];
    }

    NSString *loginName = [[NSUserDefaults standardUserDefaults]objectForKey:@"LoginName"];
    NSDictionary *dic = @{@"Tel":loginName,@"OrderType":@"2",@"OrderFrom":@"1"};
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Api.GetMsm" withParaments:dic withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
        if ([object[@"Code"] isEqualToString:@"00000"]) {
            [ToastView showInfoWithStatus:@"短信已发送"];
            phone = [NSString stringWithFormat:@"%@",object[@"Data"][0][@"VerificationCode"]];
        }else {
            [ToastView showInfoWithStatus:@"短信发送失败"];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:nil];
}

-(void)textFieldDidEndEditing:(UITextField *)textField {
    float money = [self.OutBenjinLB.text floatValue]+[self.OutShouyi.text floatValue];
    self.OutSubMoney.text = [NSString stringWithFormat:@"%.2f",money];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return [textField resignFirstResponder];
}

- (IBAction)sureBtn:(id)sender {
    if ([self.phoneNumber.text isEqualToString:@""]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"请输入验证码" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
    
    if (![self.phoneNumber.text isEqualToString:phone]) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"验证码错误" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }

    
      NSString *loginId = [[NSUserDefaults standardUserDefaults] objectForKey:@"LoginId"];
    
    NSLog(@"%.2f %.2f %.2f %.2f",[self.OutBenjinLB.text floatValue],[self.BenjinLB.text floatValue],[self.OutShouyi.text floatValue],[self.TransferableLix floatValue] );
    
    if (([self.OutBenjinLB.text floatValue] > [self.BenjinLB.text floatValue]) || ([self.OutShouyi.text floatValue] > [self.TransferableLix floatValue])) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"输入金额大于拥有金额" delegate: nil cancelButtonTitle:@"确定" otherButtonTitles: nil, nil];
        [alert show];
        return;
    }
      NSDictionary *para = @{
                           @"BorrowListId":self.BorrowListId,
                           @"Borrowid":self.Borrowid,
                           @"Detail":@"急需用钱",
                           @"Amount":self.OutBenjinLB.text,
                           @"TransfeLiXi":self.OutShouyi.text,
                           @"TransferableAmount":[NSString stringWithFormat:@"%.2f", [self.TransferableAmount doubleValue]],
                           @"TransferableLix":[NSString stringWithFormat:@"%.2f", [self.TransferableLix doubleValue]],
                           @"LoginId":loginId
                           };
    
    [NetWorkManager requestWithType:HttpRequestTypePost withMethodName:@"Transfer.GetAddTransfer" withParaments:para withSuccessBlock:^(NSDictionary *object) {
        NSLog(@"%@",object);
        if ([object[@"Code"] isEqualToString:@"00000"]) {
            [ToastView showInfoWithStatus:@"操作成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else {
            [ToastView showInfoWithStatus:@"操作失败"];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        [ToastView showErrorWithStatus:@"网络请求失败"];
    } progress:nil];
}

//定时器每隔一秒调用的方法
- (void)getValidateCodeCountDown
{
    if (countDownSeconds >= 0) {
        [_getCOde setTitle:[NSString stringWithFormat:@"剩余%lds",(long)countDownSeconds] forState:UIControlStateNormal];
        countDownSeconds--;
    }else{
        countDownSeconds = 60;
        _getCOde.selected = NO;
        _getCOde.enabled = YES;
        //        _getCOde.backgroundColor = [UIColor colorWithRed:0.93 green:0.37 blue:0.31 alpha:1.00];
        [_getCOde setTitle:@"获取验证码" forState:UIControlStateNormal];
        [_timer setFireDate:[NSDate distantFuture]];
    }
}


@end
